package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.example.system.entity.AppUserEntity;
import org.example.system.entity.CollectionEntity;
import org.example.system.entity.MediaEntity;
import org.example.system.mapper.CollectionMapper;
import org.example.system.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * 合集服务实现
 */
@Service
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    private CollectionMapper collectionMapper;

    /**
     * 按条件分页查询
     * @param collection
     * @return
     */
    @Override
    public IPage<CollectionEntity> selectPage(CollectionEntity collection){
        MPJLambdaWrapper<CollectionEntity> wrapper = new MPJLambdaWrapper<>();
        if (collection.getId() != null){
            wrapper.eq(CollectionEntity:: getId,collection.getId());
        }
        //条件
        if (StrUtil.isNotEmpty(collection.getMediaTitle())){
            wrapper.like(CollectionEntity:: getMediaTitle,collection.getMediaTitle());    //相似的合集名（模糊查询）
        }
        if (StrUtil.isNotEmpty(collection.getMediaAuthor())){
            wrapper.like(CollectionEntity:: getMediaAuthor,collection.getMediaAuthor());    //相似的作者昵称（模糊查询）
        }
        if (StrUtil.isNotEmpty(collection.getNickname())){
            wrapper.like(AppUserEntity:: getNickname,collection.getNickname());    //相似的上传者昵称（模糊查询）
        }
        if (collection.getMediaType() != null){
            wrapper.eq(CollectionEntity:: getMediaType,collection.getMediaType());    //类型
        }
        if (StrUtil.isNotEmpty(collection.getDesc())){
            wrapper.orderByDesc(CollectionEntity::getCreateTime);    //倒序
        }
        if (collection.getViews() != null){
            wrapper.orderByDesc(CollectionEntity::getViews);
        }
        if (collection.getCuration() != null){
            wrapper.orderByDesc(CollectionEntity::getCuration);
        }
        wrapper.selectAll(CollectionEntity.class);
        wrapper.leftJoin(AppUserEntity.class, AppUserEntity::getId, CollectionEntity::getUploaderId);
        wrapper.selectAs(AppUserEntity::getNickname, "nickname");
        IPage<CollectionEntity> page = new Page<>();
        page.setCurrent(collection.getPageNum());
        page.setSize(collection.getPageSize());
        return collectionMapper.selectPage(page,wrapper);
    }

    /**
     * 添加合集
     * @param collection
     * @return
     */
    @Override
    public int add(CollectionEntity collection) {
        collection.setCreateTime(new Date());
        return collectionMapper.insert(collection);
    }

    /**
     * 修改
     *
     * @param collection
     * @return
     */
    @Override
    public int update(CollectionEntity collection) {
        collection.setUpdateTime(new Date());
        return collectionMapper.updateById(collection);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return collectionMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据id获取信息
     *
     * @param id
     * @return
     */
    @Override
    public CollectionEntity getOne(Long id) {
        return collectionMapper.selectById(id);
    }
}
